package com.qiwenshare.file.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qiwenshare.common.result.RestResult;
import com.qiwenshare.file.domain.ClickRecord;
import com.qiwenshare.file.domain.SystemDO;
import com.qiwenshare.file.service.ClickRecordService;
import com.qiwenshare.file.service.SystemService;
import com.qiwenshare.file.vo.ClickRecordVO;
import com.qiwenshare.file.vo.SystemVO;
import com.qiwenshare.file.vo.file.UploadFileVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CZF
 * @create 2024-01-16 0:27
 */
@Slf4j
@RestController
@RequestMapping("/system")
//@CrossOrigin
public class SystemController {

    @Autowired
    SystemService systemService;

    @Autowired
    ClickRecordService clickRecordService;

    /**
     * 系统监控总数，30页一条
     * @return
     */
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    @ResponseBody
    public RestResult<List<SystemVO>> getTotal(){

        Page<SystemDO> page = new Page<>();
        page.setPages(1);
        page.setSize(30);
        List<SystemDO> list = systemService.page(page).getRecords();

        return RestResult.success().data(list.stream().map(SystemVO::valueOf).collect(Collectors.toList()));
    }

    /**
     * 网站
     * @return
     */
    @GetMapping(value = "/clickRecord")
    @ResponseBody
    public RestResult<ClickRecordVO> clickRecord(){

        ClickRecordVO vo = new ClickRecordVO();

        // 获取今日的起始时间和结束时间
        LocalDateTime startOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endOfDay = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);

        // 构建查询条件
        LambdaQueryWrapper<ClickRecord> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClickRecord::getType, "点击")
                .between(ClickRecord::getTime, startOfDay, endOfDay);

        vo.setDayActive(clickRecordService.count(wrapper));


        // 获取本月的起始时间和结束时间
        LocalDateTime startOfMonth = LocalDateTime.of(LocalDate.now().withDayOfMonth(1), LocalTime.MIN);
        LocalDateTime endOfMonth = LocalDateTime.of(LocalDate.now().withDayOfMonth(LocalDate.now().lengthOfMonth()), LocalTime.MAX);

        // 构建查询条件
        LambdaQueryWrapper<ClickRecord> wrapper2 = new LambdaQueryWrapper<>();
        wrapper.eq(ClickRecord::getType, "点击")
                .between(ClickRecord::getTime, startOfMonth, endOfMonth);
        vo.setMonthActive(clickRecordService.count(wrapper2));


        LambdaQueryWrapper<ClickRecord> wrapper3 = new LambdaQueryWrapper<>();
        wrapper.eq(ClickRecord::getType, "点击");

        vo.setPv(clickRecordService.count(wrapper3));
        return RestResult.success().data(vo);

    }
}
